package com.bnlr.lrparking.appui.sharevalue.recharge;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.RechargeListAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.RechargeListM;
import com.bnlr.lrparking.widget.refresh.RefreshHolderUtil;
import com.bnlr.lrparking.widget.refresh.XRefreshLayout;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 充值记录
 *
 * @author hp
 * @date 2017/8/31
 */

public class RechargeRecordActivity extends BaseActivity {
    @BindView(R.id.tv_nav_title)
    TextView tv_nav_title;
    @BindView(R.id.swipeLayout)
    XRefreshLayout swipeLayout;
    @BindView(R.id.rv_recharge)
    RecyclerView recyclerView;
    private RechargeListAdapter adapter;
    private List<RechargeListM.DataBean> list = new ArrayList<>();
    private Unbinder unbinder;
    private int currentPage = 1;
    @BindView(R.id.no_recharge_data)
    RelativeLayout no_data;
    private DecimalFormat df = new DecimalFormat("######0.00");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_list);
        unbinder = ButterKnife.bind(this);
        initView();
        getRechargeList("refresh", currentPage);
    }

    private void getRechargeList(final String handAction, int currentPage) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "GetAccountRecord");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("pageIndex", currentPage);
            mRequest.add("recordType", 5);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<RechargeListM>(baseContext, true, RechargeListM.class) {
                        @Override
                        public void doWork(RechargeListM data, String code) {
                            if (TextUtils.equals("1", code)) {
                                if (swipeLayout != null && adapter != null) {
                                    swipeLayout.endRefreshing();
                                    if (data != null && data.getData().size() != 0) {
                                        for (int i = 0; i < data.getData().size(); i++) {
                                            data.getData().get(i).setMoney("¥" + df.format(Double.valueOf((Double.parseDouble(data.getData().get(i).getShareValue() + "") / LrConstant.RATE_VALUE) + 0.005)));
                                        }
                                        if (handAction.equals("refresh")) {
                                            no_data.setVisibility(View.GONE);
                                            list = data.getData();
                                            LogUtils.e(list.toArray());
                                            adapter.setNewData(data.getData());
                                        } else {
                                            list.addAll(data.getData());
                                            adapter.addData(data.getData());
                                        }
                                        adapter.loadMoreComplete();
                                    } else {
                                        if (handAction.equals("refresh")) {
                                            no_data.setVisibility(View.VISIBLE);
                                        } else {
                                            adapter.loadMoreEnd();
                                        }

                                    }
                                }
                            } else {
                                ToastUtils.showShort(data.getMsg() + "");
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {
        }
    }

    private void initView() {
        tv_nav_title.setText("充值记录");
        swipeLayout.setRefreshViewHolder(RefreshHolderUtil.getRefreshHolder(this));
        swipeLayout.setDelegate(new XRefreshLayout.BGARefreshLayoutDelegate() {
            @Override
            public void onBGARefreshLayoutBeginRefreshing(XRefreshLayout refreshLayout) {
                currentPage = 1;
                getRechargeList("refresh", currentPage);
            }

            @Override
            public boolean onBGARefreshLayoutBeginLoadingMore(XRefreshLayout refreshLayout) {
                return false;
            }
        });
        swipeLayout.beginRefreshing();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RechargeListAdapter(list);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage++;
                getRechargeList("loadMore", currentPage);
            }
        }, recyclerView);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(RechargeRecordActivity.this, RechargeDetailsActivity.class);
                intent.putExtra("recharge_detail", list.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }


}
